package com.qk.manangement.controller;

import com.qk.common.response.PageResult;
import com.qk.common.response.Result;
import com.qk.common.response.UserByLabelResponse;
import com.qk.entity.pojo.User;
import com.qk.manangement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     */
    @GetMapping
    public Result list(String name, String phone, Integer status, Integer deptId,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult<UserByLabelResponse> userPageResult = userService.getUserList(name, phone, status, deptId, page, pageSize);
        return Result.success(userPageResult);
    }

    /**
     * 根据id删除用户
     * @param id 用户id
     *  删除结果
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean success = userService.deleteUser(id);
        if (!success) {
            return Result.error("删除用户失败");
        }
        return Result.success();
    }

    /**
     * 添加用户
     * @param user 用户信息
     * @return 添加结果
     */
    @PostMapping
    public Result add(@RequestBody User user) {
        boolean success = userService.addUser(user);
        if (!success) {
            return Result.error("添加用户失败");
        }
        return Result.success();
    }

    /**
     * 根据id查询用户
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        if (user == null){
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * @param user 用户信息
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        boolean success = userService.updateUser(user);
        if (!success) {
            return Result.error("修改用户失败");
        }
        return Result.success();
    }

    /**
     * 根据标识查询用户列表
     */
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        List<UserByLabelResponse> userListByLabelResult = userService.getUserListByLabel(roleLabel);
        return Result.success(userListByLabelResult);
    }

    /**
     * 根据部门id查询用户列表
     */
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        List<UserByLabelResponse> userListByDeptResult = userService.getUserListByDept(deptId);
        return Result.success(userListByDeptResult);
    }

    /**
     * 查询所有用户列表
     */
    @GetMapping("/list")
    public Result list() {
        List<UserByLabelResponse> userList = userService.listMy();
        return Result.success(userList);
    }





}
